import createFSA from '@/shared/utils/createFSA';
import * as API from '@/api';

const NAMESPACE = 'sign';
const LOGIN_EFFECT = 'LOGIN/LOGIN_EFFECT';
const SET_ERROR_UI = 'LOGIN/SET_ERROR_UI';

export const signInEffectAction = createFSA(`${NAMESPACE}/${LOGIN_EFFECT}`);
export const setErrorUiAction = createFSA(`${NAMESPACE}/${SET_ERROR_UI}`);

const Model = {
  namespace: NAMESPACE,
  state: '',

  effects: {
    * [LOGIN_EFFECT]({ payload }, { call }) {

      try {
        // const response1 = yield call(API.getUserName, { data: { name: 'asdfasfd' } });
        // let cancel;
        // const config = {
        //   cancelToken: new CancelToken(function executor(c) {
        //     // executor 函数接收一个 cancel 函数作为参数
        //     cancel = c;
        //   })
        // };

        // setTimeout(() => {
        //   cancel();
        // }, 1);
        const response = yield call(
          API.login, { pathParams: { id: 2323 }, data: payload });
        console.log('response', response);

      } catch (res) {
        // put(setErrorUiAction(res));
        console.log('error response', res);
      }
    }
  },

  reducers: {
    [SET_ERROR_UI](state) {
      return state.withMutations(s => {
        s.updateIn(['text', 2], v => v + 1);
        s.updateIn(['persons', 'detail', 'name'], () => '张三');
      });
    }
  }
};

export default Model;
